package com.zit.presentation.action;

import java.util.List;

import com.zit.entity.Dept;
import java.io.IOException;
import java.util.List;


import com.zit.entity.Classes;
import com.zit.entity.Teacher;
import com.zit.service.TeacherService;

/**
 * 教师管理Action
 * 
 * @author 韩超
 * @createDate 2013-12-03
 */
@SuppressWarnings("serial")
public class TeacherAction extends BaseAction {

	TeacherService ts = new TeacherService();

	// 获取界面传过来的id
	private Integer id;

	// 获取修改教师部门功能的部门id
	private Integer deptid;

	private Teacher teacher;

	/**
	 * 查询所有老师的信息
	 * 
	 * @return
	 */
	public String selectdept() {
		requestMap.put("dept", ts.selectdept());
		requestMap.put("teacher", ts.selectteacher());
		return "deptsucces";
	}

	/**
	 * 根据部门查询老师
	 * 
	 * @return
	 */
	public String selectteacher() {
		requestMap.put("dept", ts.selectdept());
		requestMap.put("teacher", ts.selectteacher(deptid));
		return "deptidsucces";
	}

	/**
	 * 修改教师之前查询教师信息
	 * 
	 * @return
	 */
	public String updateselectteacher() {
		requestMap.put("dept", ts.selectdept());
		requestMap.put("teacher", ts.idselectteacher(id));
		return "updateselectteacher";
	}
	
	/**
	 * 修改教师信息
	 * @return
	 */
	public String updateteacher(){
		ts.updateteacherdept(teacher);
		return "updateteacher";
	}

	/**
	 * 添加教师之前查询部门
	 * 
	 * @return
	 */
	public String addselectteacher() {
		requestMap.put("dept", ts.selectdept());
		return "addselect";
	}

	/**
	 * 添加教师
	 * 
	 * @return
	 */
	public String addteacher() {
		ts.save(teacher);
		return "addteacher";
	}

	/**
	 * 根据id删除教师信息
	 * 
	 * @return
	 */
	public String deleteteacher() {
		if(classesids==null || classesids.length==0){
			ts.deleteteacher(teacher.getId());
			return "deleteteacher";
		}else{
			ts.deleteteacher(teacher,classesids,teacherids);
			return NONE ;
		}
	}

	/**
	 * @return
	 * @throws IOException 
	 */
	public String countClasses() throws IOException {
		int countCla = ts.countClasses(teacher.getId());
		response.getWriter().print(countCla);
		return NONE;
	}

	public String changeTeaForClasses() throws Exception{
		teacher = (Teacher) ts.findByteaching(teacher.getId());
		if(teacher.getIsDelete()==1){
			throw new Exception();
		}
		requestMap.put("teaching", teacher);
		return "changeTeaForClasses" ;
	}
	
	public String findChangeTeaForDelTeacher() throws IOException{
		//System.out.println(teacher.getId()+" "+teacher.getIsPro()+" "+teacher.getDept().getId());
		List<Teacher> teas = ts.findChangeTeaForDelTeacher(teacher);
		StringBuilder sb = new StringBuilder("([");
		for (Teacher teacher : teas) {
			sb.append("{id:"+teacher.getId()+",name:'"+teacher.getName()+"'},");
		}
		String s = sb.substring(0, sb.length()-1)+"])";
		response.getWriter().print(s);
		return null ;
	}
	
	private int[] teacherids;
	private int[] classesids;
	
	
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public Integer getDeptid() {
		return deptid;
	}

	public void setDeptid(Integer deptid) {
		this.deptid = deptid;
	}

	public Teacher getTeacher() {
		return teacher;
	}

	public void setTeacher(Teacher teacher) {
		this.teacher = teacher;
	}

	public int[] getTeacherids() {
		return teacherids;
	}

	public void setTeacherids(int[] teacherids) {
		this.teacherids = teacherids;
	}

	public int[] getClassesids() {
		return classesids;
	}

	public void setClassesids(int[] classesids) {
		this.classesids = classesids;
	}

}
